package com.yanqu.road.server.manger.activity.fouractivity;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.share.config.ShareConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eFourActivityChildType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.pb.activity.ShareProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/8/4 16:45
 */
public class FourActivityMgr extends TempMgr {
    private static Map<Integer, String> rewardConfigMap = new ConcurrentHashMap<>(); //childType
    private static Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();  //childType
    private static ShareConfig shareConfig;


    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static void reloadActivityData(){
        getLogger().info("reload fourActivity activity start");
        // 活动信息 activityInfo
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.FourActivity.getValue());
        if (openActivityInfoList.size() == 0) {
            getLogger().info("no fourActivity activity in show time");
            return;
        }
        for (int i = 1; i <= 4; i++) {
            for (ActivityInfo activityInfo : openActivityInfoList) {
                if (activityInfo.getChildType() == i) {
                    activityInfoMap.put(i, activityInfo);
                    break;
                }
            }
        }

        for (Map.Entry<Integer, ActivityInfo> entry : activityInfoMap.entrySet()) {
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(entry.getValue().getActivityId());
            if (entry.getKey() == eFourActivityChildType.Share.getChildType()) {
                ShareConfig tmpConfig = new ShareConfig();
                tmpConfig.setActivityId(entry.getValue().getActivityId());
                tmpConfig.setActivityInfo(entry.getValue());
                tmpConfig.setShareCD(Integer.parseInt(activityConfigMap.get("FXYL_SHARE_CD").getValue()));
                tmpConfig.setShareDailyLimit(Integer.parseInt(activityConfigMap.get("FXYL_SHARE_DAY_LIMIT").getValue()));
                tmpConfig.setShareReward(activityConfigMap.get("FXYL_SHARE_REWARD").getValue());
                tmpConfig.setInvitationCD(Integer.MAX_VALUE); // 这活动没这个配置
                // 设置config
                shareConfig = tmpConfig;
            } else if (entry.getKey() == eFourActivityChildType.Follow.getChildType()) {
                rewardConfigMap.put(entry.getKey(), activityConfigMap.get("GZYL_SHARE_REWARD").getValue());
            } else if (entry.getKey() == eFourActivityChildType.RealName.getChildType()) {
                rewardConfigMap.put(entry.getKey(), activityConfigMap.get("SMRZ_SHARE_REWARD").getValue());
            } else if (entry.getKey() == eFourActivityChildType.SaveDesk.getChildType()) {
                rewardConfigMap.put(entry.getKey(), activityConfigMap.get("BCZM_SHARE_REWARD").getValue());
            }
        }

        getLogger().info("reload fourActivity activity finish");
    }

    public static ActivityInfo getActivityInfo(int childType) {
        return activityInfoMap.get(childType);
    }

    public static ShareConfig getShareConfig() {
        return shareConfig;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public synchronized static int getReward(int childType, GamePlayer player) {
        if (!activityInfoMap.containsKey(childType)) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        ePlayerAttrType childTypeAttr = getChildTypeAttr(childType);
        if (childTypeAttr == null) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        long rewardState = player.getModule(AttributeModule.class).getAttribute(childTypeAttr);
        if (rewardState == 1) {
            return GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
        }
        if (!rewardConfigMap.containsKey(childType)) {
            return GameErrorCode.E_REWARD_CONFIG_NO_FOUND;
        }
        String reward = rewardConfigMap.get(childType);
        //领奖 设置领奖状态
        player.getModule(AttributeModule.class).setAttribute(childTypeAttr, 1);
        player.getModule(CurrencyModule.class).addCurrency(PropertyHelper.parseStringToProperty(reward), eLogMoneyType.FourActivity, eLogMoneyType.FourActivityReward);
        ShareProto.FourActivityGetRewardRespMsg.Builder respMsg = ShareProto.FourActivityGetRewardRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setReward(reward);
        player.sendPacket(ClientProtocol.U_FOUR_ACTIVITY_OP_SUCCESS_REWARD, respMsg);
        return 0;
    }

    public static ePlayerAttrType getChildTypeAttr(int childType) {
        if (childType == eFourActivityChildType.Follow.getChildType()) {
            return ePlayerAttrType.FollowReward;
        } else if (childType == eFourActivityChildType.RealName.getChildType()) {
            return ePlayerAttrType.RealNameReward;
        } else if (childType == eFourActivityChildType.SaveDesk.getChildType()) {
            return ePlayerAttrType.SaveDeskReward;
        }
        return null;
    }
}
